﻿using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;

namespace Sonce.Editor.Tools
{
    //Under development
    public partial class ViewTool : ViewHelper, IRawTool
    {
        #region Members
        private bool isMouseDown = false;
        private Editor editor;
        #endregion

        #region Constructor
        /// <summary>
        /// Initializes a new ViewTool instance.
        /// </summary>
        public ViewTool()
        {
        }
        #endregion

        #region ITool Members
        public Editor Editor
        {
            get
            {
                return this.editor;
            }
            set
            {
                this.editor = value;
                base.Container = this.editor;
                if (this.editor.Parent is FrameworkElement)
                {
                    FrameworkElement parent = this.editor.Parent as FrameworkElement;
                    parent.MouseLeftButtonDown += new MouseButtonEventHandler(parent_MouseLeftButtonDown);
                    parent.MouseLeftButtonUp += new MouseButtonEventHandler(parent_MouseLeftButtonUp);
                    parent.MouseMove += new MouseEventHandler(parent_MouseMove);
                }
            }
        }

        void parent_MouseMove(object sender, MouseEventArgs e)
        {
            if (isMouseDown)
            {
                Point position = e.GetPosition(sender as FrameworkElement);
                //Point position = e.GetPosition(element);
                Point v = new Point(start.X - position.X, start.Y - position.Y);
                PanTo(new Point(origin.X - v.X, origin.Y - v.Y));

                //base.PanBy(position, 500);
            }
        }

        void parent_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            MouseUp(sender, e);
        }

        void parent_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            MouseDown(sender, e);
        }
        #endregion

        #region IRawTool Members
        public bool KeyDown(object sender, KeyEventArgs e)
        {
            return false;
        }

        public bool KeyUp(object sender, KeyEventArgs e)
        {
            return false;
        }

        public bool MouseDown(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = true;
            start = e.GetPosition(sender as FrameworkElement);
            origin = new Point(tt.X, tt.Y);
            return true;
        }

        public bool MouseUp(object sender, MouseButtonEventArgs e)
        {
            isMouseDown = false;
            return true;
        }

        public bool MouseMove(object sender, MouseEventArgs e)
        {
            return false;
        }

        public bool MouseEnter(object sender, MouseEventArgs e)
        {
            return false;
        }

        public bool MouseLeave(object sender, MouseEventArgs e)
        {
            return false;
        }

        public bool MouseWheel(object sender, EventArgs<int> e)
        {
            //TODO: Zooming is not linear, improve UX
            double amount = e.Result * 0.2;
            ZoomBy(amount);

            //double zoom = (e.Delta > 0 ? 1.5 : 0.5);
            //Zoom(zoom, e.GetPosition(this.container), e.GetPosition(element));

            return true;
        }
        #endregion
    }
}
